/*
 * Copyright (C) 2020 The LINN Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.juqimiao.linn.command;

import com.juqimiao.linn.exception.FrameworkException;
import com.juqimiao.linn.interception.InterceptorKeeperI;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 储存相关命令信息的命令中心。
 *
 * 它的主要作用是保存所有命令和命令执行器类的信息。
 * 实际调用中，将从这个命令中心中寻找并进行执行。
 *
 * @author  Collin Cai
 */
@Component
public class CommandHub {
    /* ---- Static ---------- */

    private static final String ERROR_TEMPLATE = "%s is not registered in CommandHub, please register it first.";

    /* ---- Fields ---------- */

    @Getter
    @Setter
    private Map<Class, CommandInvocation> commandKeeper = new HashMap<>();
    
    /**
     * Todo: 考虑将 Responses 和 Commands 的关系改变。
     */
    @Getter
    @Setter
    private Map<Class, Class> responseKeeper = new HashMap<>();


    /* ---- Methods ---------- */

    public CommandInvocation getCommandInvocation(Class cmdClass) {
        CommandInvocation commandInvocation = commandKeeper.get(cmdClass);

        if (commandKeeper.get(cmdClass) == null) {
            String msg = String.format(ERROR_TEMPLATE, cmdClass);
            throw new FrameworkException(msg);
        }
        return commandInvocation;
    }
}
